
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.utils.html &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-html">
            
  <h1>Source code for django.utils.html</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;HTML utilities suitable for global use.&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">html.parser</span> <span class="k">import</span> <span class="n">HTMLParser</span>
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">parse_qsl</span><span class="p">,</span> <span class="n">quote</span><span class="p">,</span> <span class="n">unquote</span><span class="p">,</span> <span class="n">urlencode</span><span class="p">,</span> <span class="n">urlsplit</span><span class="p">,</span> <span class="n">urlunsplit</span><span class="p">,</span>
<span class="p">)</span>

<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">Promise</span><span class="p">,</span> <span class="n">keep_lazy</span><span class="p">,</span> <span class="n">keep_lazy_text</span>
<span class="kn">from</span> <span class="nn">django.utils.http</span> <span class="k">import</span> <span class="n">RFC3986_GENDELIMS</span><span class="p">,</span> <span class="n">RFC3986_SUBDELIMS</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="k">import</span> <span class="n">SafeData</span><span class="p">,</span> <span class="n">SafeText</span><span class="p">,</span> <span class="n">mark_safe</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="k">import</span> <span class="n">normalize_newlines</span>

<span class="c1"># Configuration for urlize() function.</span>
<span class="n">TRAILING_PUNCTUATION_CHARS</span> <span class="o">=</span> <span class="s1">&#39;.,:;!&#39;</span>
<span class="n">WRAPPING_PUNCTUATION</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;(&#39;</span><span class="p">,</span> <span class="s1">&#39;)&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;[&#39;</span><span class="p">,</span> <span class="s1">&#39;]&#39;</span><span class="p">)]</span>

<span class="c1"># List of possible strings used for bullets in bulleted lists.</span>
<span class="n">DOTS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&amp;middot;&#39;</span><span class="p">,</span> <span class="s1">&#39;*&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\u2022</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&amp;#149;&#39;</span><span class="p">,</span> <span class="s1">&#39;&amp;bull;&#39;</span><span class="p">,</span> <span class="s1">&#39;&amp;#8226;&#39;</span><span class="p">]</span>

<span class="n">unencoded_ampersands_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;&amp;(?!(\w+|#\d+);)&#39;</span><span class="p">)</span>
<span class="n">word_split_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;&#39;&#39;([\s&lt;&gt;&quot;&#39;]+)&#39;&#39;&#39;</span><span class="p">)</span>
<span class="n">simple_url_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^https?://\[?\w&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
<span class="n">simple_url_2_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)($|/.*)$&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>

<span class="n">_html_escapes</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&amp;&#39;</span><span class="p">):</span> <span class="s1">&#39;&amp;amp;&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span><span class="p">):</span> <span class="s1">&#39;&amp;lt;&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&gt;&#39;</span><span class="p">):</span> <span class="s1">&#39;&amp;gt;&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&quot;&#39;</span><span class="p">):</span> <span class="s1">&#39;&amp;quot;&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;&#39;&quot;</span><span class="p">):</span> <span class="s1">&#39;&amp;#39;&#39;</span><span class="p">,</span>
<span class="p">}</span>


<div class="viewcode-block" id="escape"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.escape">[docs]</a><span class="nd">@keep_lazy</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">SafeText</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">escape</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the given text with ampersands, quotes and angle brackets encoded</span>
<span class="sd">    for use in HTML.</span>

<span class="sd">    Always escape input, even if it&#39;s already escaped and marked as such.</span>
<span class="sd">    This may result in double-escaping. If this is a concern, use</span>
<span class="sd">    conditional_escape() instead.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">text</span><span class="p">)</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">_html_escapes</span><span class="p">))</span></div>


<span class="n">_js_escapes</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u005C&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\&#39;</span><span class="s1">&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u0027&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&quot;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u0022&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&gt;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u003E&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u003C&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&amp;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u0026&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u003D&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;-&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u002D&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u003B&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;`&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u0060&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\u2028</span><span class="s1">&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u2028&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\u2029</span><span class="s1">&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u2029&#39;</span>
<span class="p">}</span>

<span class="c1"># Escape every ASCII character with a value less than 32.</span>
<span class="n">_js_escapes</span><span class="o">.</span><span class="n">update</span><span class="p">((</span><span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%c</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">z</span><span class="p">),</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u</span><span class="si">%04X</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">z</span><span class="p">)</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">32</span><span class="p">))</span>


<span class="nd">@keep_lazy</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">SafeText</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">escapejs</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Hex encode characters for use in JavaScript strings.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">_js_escapes</span><span class="p">))</span>


<span class="n">_json_script_escapes</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&gt;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u003E&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u003C&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;&amp;&#39;</span><span class="p">):</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">u0026&#39;</span><span class="p">,</span>
<span class="p">}</span>


<span class="k">def</span> <span class="nf">json_script</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">element_id</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Escape all the HTML/XML special characters with their unicode escapes, so</span>
<span class="sd">    value is safe to be output anywhere except for inside a tag attribute. Wrap</span>
<span class="sd">    the escaped JSON in a script tag.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">from</span> <span class="nn">django.core.serializers.json</span> <span class="k">import</span> <span class="n">DjangoJSONEncoder</span>
    <span class="n">json_str</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="n">DjangoJSONEncoder</span><span class="p">)</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">_json_script_escapes</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">format_html</span><span class="p">(</span>
        <span class="s1">&#39;&lt;script id=&quot;</span><span class="si">{}</span><span class="s1">&quot; type=&quot;application/json&quot;&gt;</span><span class="si">{}</span><span class="s1">&lt;/script&gt;&#39;</span><span class="p">,</span>
        <span class="n">element_id</span><span class="p">,</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">json_str</span><span class="p">)</span>
    <span class="p">)</span>


<div class="viewcode-block" id="conditional_escape"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.conditional_escape">[docs]</a><span class="k">def</span> <span class="nf">conditional_escape</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Similar to escape(), except that it doesn&#39;t operate on pre-escaped strings.</span>

<span class="sd">    This function relies on the __html__ convention used both by Django&#39;s</span>
<span class="sd">    SafeData class and by third-party libraries like markupsafe.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">Promise</span><span class="p">):</span>
        <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="s1">&#39;__html__&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">text</span><span class="o">.</span><span class="n">__html__</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">escape</span><span class="p">(</span><span class="n">text</span><span class="p">)</span></div>


<div class="viewcode-block" id="format_html"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.format_html">[docs]</a><span class="k">def</span> <span class="nf">format_html</span><span class="p">(</span><span class="n">format_string</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Similar to str.format, but pass all arguments through conditional_escape(),</span>
<span class="sd">    and call mark_safe() on the result. This function should be used instead</span>
<span class="sd">    of str.format or % interpolation to build up small HTML fragments.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">args_safe</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">conditional_escape</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
    <span class="n">kwargs_safe</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">conditional_escape</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">format_string</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="n">args_safe</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs_safe</span><span class="p">))</span></div>


<div class="viewcode-block" id="format_html_join"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.format_html_join">[docs]</a><span class="k">def</span> <span class="nf">format_html_join</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="n">format_string</span><span class="p">,</span> <span class="n">args_generator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A wrapper of format_html, for the common case of a group of arguments that</span>
<span class="sd">    need to be formatted using the same format string, and then joined using</span>
<span class="sd">    &#39;sep&#39;. &#39;sep&#39; is also passed through conditional_escape.</span>

<span class="sd">    &#39;args_generator&#39; should be an iterator that returns the sequence of &#39;args&#39;</span>
<span class="sd">    that will be passed to format_html.</span>

<span class="sd">    Example:</span>

<span class="sd">      format_html_join(&#39;\n&#39;, &quot;&lt;li&gt;{} {}&lt;/li&gt;&quot;, ((u.first_name, u.last_name)</span>
<span class="sd">                                                  for u in users))</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">conditional_escape</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
        <span class="n">format_html</span><span class="p">(</span><span class="n">format_string</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">args</span> <span class="ow">in</span> <span class="n">args_generator</span>
    <span class="p">))</span></div>


<span class="nd">@keep_lazy_text</span>
<span class="k">def</span> <span class="nf">linebreaks</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">autoescape</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Convert newlines into &lt;p&gt; and &lt;br&gt;s.&quot;&quot;&quot;</span>
    <span class="n">value</span> <span class="o">=</span> <span class="n">normalize_newlines</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="n">paras</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">{2,}&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">autoescape</span><span class="p">:</span>
        <span class="n">paras</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&lt;p&gt;</span><span class="si">%s</span><span class="s1">&lt;/p&gt;&#39;</span> <span class="o">%</span> <span class="n">escape</span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;br&gt;&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">paras</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">paras</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&lt;p&gt;</span><span class="si">%s</span><span class="s1">&lt;/p&gt;&#39;</span> <span class="o">%</span> <span class="n">p</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;br&gt;&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">paras</span><span class="p">]</span>
    <span class="k">return</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">paras</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">MLStripper</span><span class="p">(</span><span class="n">HTMLParser</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">convert_charrefs</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">handle_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle_entityref</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;&amp;</span><span class="si">%s</span><span class="s1">;&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle_charref</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;&amp;#</span><span class="si">%s</span><span class="s1">;&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_strip_once</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Internal tag stripping utility used by strip_tags.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">MLStripper</span><span class="p">()</span>
    <span class="n">s</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="n">s</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">s</span><span class="o">.</span><span class="n">get_data</span><span class="p">()</span>


<div class="viewcode-block" id="strip_tags"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.strip_tags">[docs]</a><span class="nd">@keep_lazy_text</span>
<span class="k">def</span> <span class="nf">strip_tags</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return the given HTML with all tags stripped.&quot;&quot;&quot;</span>
    <span class="c1"># Note: in typical case this loop executes _strip_once once. Loop condition</span>
    <span class="c1"># is redundant, but helps to reduce number of executions of _strip_once.</span>
    <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">while</span> <span class="s1">&#39;&lt;&#39;</span> <span class="ow">in</span> <span class="n">value</span> <span class="ow">and</span> <span class="s1">&#39;&gt;&#39;</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
        <span class="n">new_value</span> <span class="o">=</span> <span class="n">_strip_once</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">new_value</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span><span class="p">):</span>
            <span class="c1"># _strip_once wasn&#39;t able to detect more tags.</span>
            <span class="k">break</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">new_value</span>
    <span class="k">return</span> <span class="n">value</span></div>


<span class="nd">@keep_lazy_text</span>
<span class="k">def</span> <span class="nf">strip_spaces_between_tags</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return the given HTML with spaces between tags removed.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;&gt;\s+&lt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&gt;&lt;&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">smart_urlquote</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Quote a URL if it isn&#39;t already quoted.&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">unquote_quote</span><span class="p">(</span><span class="n">segment</span><span class="p">):</span>
        <span class="n">segment</span> <span class="o">=</span> <span class="n">unquote</span><span class="p">(</span><span class="n">segment</span><span class="p">)</span>
        <span class="c1"># Tilde is part of RFC3986 Unreserved Characters</span>
        <span class="c1"># https://tools.ietf.org/html/rfc3986#section-2.3</span>
        <span class="c1"># See also https://bugs.python.org/issue16285</span>
        <span class="k">return</span> <span class="n">quote</span><span class="p">(</span><span class="n">segment</span><span class="p">,</span> <span class="n">safe</span><span class="o">=</span><span class="n">RFC3986_SUBDELIMS</span> <span class="o">+</span> <span class="n">RFC3986_GENDELIMS</span> <span class="o">+</span> <span class="s1">&#39;~&#39;</span><span class="p">)</span>

    <span class="c1"># Handle IDN before quoting.</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span> <span class="o">=</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="c1"># invalid IPv6 URL (normally square brackets in hostname part).</span>
        <span class="k">return</span> <span class="n">unquote_quote</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">netloc</span> <span class="o">=</span> <span class="n">netloc</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>  <span class="c1"># IDN -&gt; ACE</span>
    <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>  <span class="c1"># invalid domain part</span>
        <span class="k">return</span> <span class="n">unquote_quote</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">query</span><span class="p">:</span>
        <span class="c1"># Separately unquoting key/value, so as to not mix querystring separators</span>
        <span class="c1"># included in query values. See #22267.</span>
        <span class="n">query_parts</span> <span class="o">=</span> <span class="p">[(</span><span class="n">unquote</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">unquote</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
                       <span class="k">for</span> <span class="n">q</span> <span class="ow">in</span> <span class="n">parse_qsl</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">keep_blank_values</span><span class="o">=</span><span class="kc">True</span><span class="p">)]</span>
        <span class="c1"># urlencode will take care of quoting</span>
        <span class="n">query</span> <span class="o">=</span> <span class="n">urlencode</span><span class="p">(</span><span class="n">query_parts</span><span class="p">)</span>

    <span class="n">path</span> <span class="o">=</span> <span class="n">unquote_quote</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
    <span class="n">fragment</span> <span class="o">=</span> <span class="n">unquote_quote</span><span class="p">(</span><span class="n">fragment</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">urlunsplit</span><span class="p">((</span><span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span><span class="p">))</span>


<span class="nd">@keep_lazy_text</span>
<span class="k">def</span> <span class="nf">urlize</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">trim_url_limit</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">nofollow</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">autoescape</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convert any URLs in text into clickable links.</span>

<span class="sd">    Works on http://, https://, www. links, and also on links ending in one of</span>
<span class="sd">    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).</span>
<span class="sd">    Links can have trailing punctuation (periods, commas, close-parens) and</span>
<span class="sd">    leading punctuation (opening parens) and it&#39;ll still do the right thing.</span>

<span class="sd">    If trim_url_limit is not None, truncate the URLs in the link text longer</span>
<span class="sd">    than this limit to trim_url_limit - 1 characters and append an ellipsis.</span>

<span class="sd">    If nofollow is True, give the links a rel=&quot;nofollow&quot; attribute.</span>

<span class="sd">    If autoescape is True, autoescape the link text and URLs.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">safe_input</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">SafeData</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">trim_url</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="n">trim_url_limit</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">limit</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">limit</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">x</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">…&#39;</span> <span class="o">%</span> <span class="n">x</span><span class="p">[:</span><span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">limit</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">unescape</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        If input URL is HTML-escaped, unescape it so that it can be safely fed</span>
<span class="sd">        to smart_urlquote. For example:</span>
<span class="sd">        http://example.com?x=1&amp;amp;y=&amp;lt;2&amp;gt; =&gt; http://example.com?x=1&amp;y=&lt;2&gt;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">text</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;&amp;amp;&#39;</span><span class="p">,</span> <span class="s1">&#39;&amp;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;&amp;lt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span>
            <span class="s1">&#39;&amp;gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&gt;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;&amp;quot;&#39;</span><span class="p">,</span> <span class="s1">&#39;&quot;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;&amp;#39;&#39;</span><span class="p">,</span> <span class="s2">&quot;&#39;&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">trim_punctuation</span><span class="p">(</span><span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Trim trailing and wrapping punctuation from `middle`. Return the items</span>
<span class="sd">        of the new state.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Continue trimming until middle remains unchanged.</span>
        <span class="n">trimmed_something</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">while</span> <span class="n">trimmed_something</span><span class="p">:</span>
            <span class="n">trimmed_something</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="c1"># Trim wrapping punctuation.</span>
            <span class="k">for</span> <span class="n">opening</span><span class="p">,</span> <span class="n">closing</span> <span class="ow">in</span> <span class="n">WRAPPING_PUNCTUATION</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">middle</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">opening</span><span class="p">):</span>
                    <span class="n">middle</span> <span class="o">=</span> <span class="n">middle</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">opening</span><span class="p">):]</span>
                    <span class="n">lead</span> <span class="o">+=</span> <span class="n">opening</span>
                    <span class="n">trimmed_something</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="c1"># Keep parentheses at the end only if they&#39;re balanced.</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">middle</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">closing</span><span class="p">)</span> <span class="ow">and</span>
                        <span class="n">middle</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">closing</span><span class="p">)</span> <span class="o">==</span> <span class="n">middle</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">opening</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                    <span class="n">middle</span> <span class="o">=</span> <span class="n">middle</span><span class="p">[:</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">closing</span><span class="p">)]</span>
                    <span class="n">trail</span> <span class="o">=</span> <span class="n">closing</span> <span class="o">+</span> <span class="n">trail</span>
                    <span class="n">trimmed_something</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="c1"># Trim trailing punctuation (after trimming wrapping punctuation,</span>
            <span class="c1"># as encoded entities contain &#39;;&#39;). Unescape entites to avoid</span>
            <span class="c1"># breaking them by removing &#39;;&#39;.</span>
            <span class="n">middle_unescaped</span> <span class="o">=</span> <span class="n">unescape</span><span class="p">(</span><span class="n">middle</span><span class="p">)</span>
            <span class="n">stripped</span> <span class="o">=</span> <span class="n">middle_unescaped</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="n">TRAILING_PUNCTUATION_CHARS</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">middle_unescaped</span> <span class="o">!=</span> <span class="n">stripped</span><span class="p">:</span>
                <span class="n">trail</span> <span class="o">=</span> <span class="n">middle</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">stripped</span><span class="p">):]</span> <span class="o">+</span> <span class="n">trail</span>
                <span class="n">middle</span> <span class="o">=</span> <span class="n">middle</span><span class="p">[:</span><span class="nb">len</span><span class="p">(</span><span class="n">stripped</span><span class="p">)</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">middle_unescaped</span><span class="p">)]</span>
                <span class="n">trimmed_something</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span>

    <span class="k">def</span> <span class="nf">is_email_simple</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if value looks like an email address.&quot;&quot;&quot;</span>
        <span class="c1"># An @ must be in the middle of the value.</span>
        <span class="k">if</span> <span class="s1">&#39;@&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value</span> <span class="ow">or</span> <span class="n">value</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">value</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">p1</span><span class="p">,</span> <span class="n">p2</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="c1"># value contains more than one @.</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="c1"># Dot must be in p2 (e.g. example.com)</span>
        <span class="k">if</span> <span class="s1">&#39;.&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">p2</span> <span class="ow">or</span> <span class="n">p2</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="n">words</span> <span class="o">=</span> <span class="n">word_split_re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">text</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">word</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">words</span><span class="p">):</span>
        <span class="k">if</span> <span class="s1">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">word</span> <span class="ow">or</span> <span class="s1">&#39;@&#39;</span> <span class="ow">in</span> <span class="n">word</span> <span class="ow">or</span> <span class="s1">&#39;:&#39;</span> <span class="ow">in</span> <span class="n">word</span><span class="p">:</span>
            <span class="c1"># lead: Current punctuation trimmed from the beginning of the word.</span>
            <span class="c1"># middle: Current state of the word.</span>
            <span class="c1"># trail: Current punctuation trimmed from the end of the word.</span>
            <span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">word</span><span class="p">,</span> <span class="s1">&#39;&#39;</span>
            <span class="c1"># Deal with punctuation.</span>
            <span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span> <span class="o">=</span> <span class="n">trim_punctuation</span><span class="p">(</span><span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span><span class="p">)</span>

            <span class="c1"># Make URL we want to point to.</span>
            <span class="n">url</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="n">nofollow_attr</span> <span class="o">=</span> <span class="s1">&#39; rel=&quot;nofollow&quot;&#39;</span> <span class="k">if</span> <span class="n">nofollow</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
            <span class="k">if</span> <span class="n">simple_url_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">middle</span><span class="p">):</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">smart_urlquote</span><span class="p">(</span><span class="n">unescape</span><span class="p">(</span><span class="n">middle</span><span class="p">))</span>
            <span class="k">elif</span> <span class="n">simple_url_2_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">middle</span><span class="p">):</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">smart_urlquote</span><span class="p">(</span><span class="s1">&#39;http://</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">unescape</span><span class="p">(</span><span class="n">middle</span><span class="p">))</span>
            <span class="k">elif</span> <span class="s1">&#39;:&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">middle</span> <span class="ow">and</span> <span class="n">is_email_simple</span><span class="p">(</span><span class="n">middle</span><span class="p">):</span>
                <span class="n">local</span><span class="p">,</span> <span class="n">domain</span> <span class="o">=</span> <span class="n">middle</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">domain</span> <span class="o">=</span> <span class="n">domain</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">url</span> <span class="o">=</span> <span class="s1">&#39;mailto:</span><span class="si">%s</span><span class="s1">@</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">local</span><span class="p">,</span> <span class="n">domain</span><span class="p">)</span>
                <span class="n">nofollow_attr</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

            <span class="c1"># Make link.</span>
            <span class="k">if</span> <span class="n">url</span><span class="p">:</span>
                <span class="n">trimmed</span> <span class="o">=</span> <span class="n">trim_url</span><span class="p">(</span><span class="n">middle</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">autoescape</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">safe_input</span><span class="p">:</span>
                    <span class="n">lead</span><span class="p">,</span> <span class="n">trail</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">lead</span><span class="p">),</span> <span class="n">escape</span><span class="p">(</span><span class="n">trail</span><span class="p">)</span>
                    <span class="n">trimmed</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">trimmed</span><span class="p">)</span>
                <span class="n">middle</span> <span class="o">=</span> <span class="s1">&#39;&lt;a href=&quot;</span><span class="si">%s</span><span class="s1">&quot;</span><span class="si">%s</span><span class="s1">&gt;</span><span class="si">%s</span><span class="s1">&lt;/a&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">escape</span><span class="p">(</span><span class="n">url</span><span class="p">),</span> <span class="n">nofollow_attr</span><span class="p">,</span> <span class="n">trimmed</span><span class="p">)</span>
                <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">safe_input</span><span class="p">:</span>
                    <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">autoescape</span><span class="p">:</span>
                    <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">safe_input</span><span class="p">:</span>
            <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">autoescape</span><span class="p">:</span>
            <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
    <span class="k">return</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">avoid_wrapping</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Avoid text wrapping in the middle of a phrase by adding non-breaking</span>
<span class="sd">    spaces where there previously were normal spaces.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\xa0</span><span class="s2">&quot;</span><span class="p">)</span>


<div class="viewcode-block" id="html_safe"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.html_safe">[docs]</a><span class="k">def</span> <span class="nf">html_safe</span><span class="p">(</span><span class="n">klass</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A decorator that defines the __html__ method. This helps non-Django</span>
<span class="sd">    templates to detect classes whose __str__ methods return SafeText.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="s1">&#39;__html__&#39;</span> <span class="ow">in</span> <span class="n">klass</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s2">&quot;can&#39;t apply @html_safe to </span><span class="si">%s</span><span class="s2"> because it defines &quot;</span>
            <span class="s2">&quot;__html__().&quot;</span> <span class="o">%</span> <span class="n">klass</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="p">)</span>
    <span class="k">if</span> <span class="s1">&#39;__str__&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">klass</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s2">&quot;can&#39;t apply @html_safe to </span><span class="si">%s</span><span class="s2"> because it doesn&#39;t &quot;</span>
            <span class="s2">&quot;define __str__().&quot;</span> <span class="o">%</span> <span class="n">klass</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="p">)</span>
    <span class="n">klass_str</span> <span class="o">=</span> <span class="n">klass</span><span class="o">.</span><span class="fm">__str__</span>
    <span class="n">klass</span><span class="o">.</span><span class="fm">__str__</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">klass_str</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
    <span class="n">klass</span><span class="o">.</span><span class="n">__html__</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">klass</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>